/*
 * Plane.java: Defines Plane objects for airline flights
 *
 * Plane is used to reserve seats on an airline. Airline seats are divided
 * into first class and economy.
 * 
 * 2008 Ron Gosa
 */

public class Plane
{ 
   /* Default constructor sets default values */
    
   public Plane() {
       firstClass = 5;            // first class, seats 0-5
       economy = 5;               // economy, seats 5-10
       numSeats = 10;             // total number seats
       seats = new boolean[numSeats];   // 0 = available.
       route = "LAX-NY";          // airline destination
       flightNum = 10102012;      // airline flight number
       numAvailable = numSeats;   // total number of seats
   }
   
   
   /* Object initialization constructor for non-default values 
        @param flight         airline flight number
        @param numFirstClass: total number of first class seats
        @param numEconomy:    total number of economy seats
        @param newRoute       next destination for current route*/
   
   public Plane(int flight, int numFirstClass, int numEconomy, String newRoute) {
       flightNum = flight;
       firstClass = numFirstClass;
       economy = numEconomy;
       numSeats = firstClass + economy;
       seats = new boolean[numSeats];
       route = newRoute;
       
       numAvailable = numSeats;
   }

   /* Reserve and airline seat. Only one argument should be true.
        @param fClass: reserve first class seat, if available
        @param eClass: reserve economy seat, if available */
        
   /* Deprecated */
   
   public void addReservation(boolean fClass, boolean eClass) {
       if(fClass && !eClass)
       {
           if(firstClass <= (numSeats - economy))
            {
               firstClass++;
               numAvailable--;
               seats[firstClass - 1] = true;
            }
       }
       
       else if(eClass && !fClass)
       {
           if(economy <= numSeats) 
            {
               economy++;
               numAvailable--;
               seats[economy - 1] = true;
            } 
       }
   }
   
   /* Reserve a FirstClass seat. */
   public boolean addFirstClassReservation() {
       for (int x = 0; x < firstClass; x++) {
           if (seats[x] == false) {
                numAvailable--;
                seats[x] = true;
                lastSeatReserved = x;
                return true;
           }
       }
       
       return false;
   }
   
   /* Reserve an Economy seat. */
   public boolean addEconomyReservation() {
       int start = firstClass - 1;
       
       for (int x = start; x < numSeats; x++) {
           if (seats[x] == false) {
                numAvailable--;
                seats[x] = true;
                lastSeatReserved = x;
                return true;
           }
       }
       
       return false;
   }
   
   
   
   /* Determine if a FirstClass seat is available, and return true or false */
   public boolean isFirstClassAvailable() {
       for (int x = 0; x < firstClass; x++) {
           if (seats[x] == false) {
               return true;
           }
       }
       
       return false;
   }
   
   /* Determine if a Economy seat is available, and return true or false */
   public boolean isEconomyAvailable() {
       int start = firstClass - 1;
       
       for (int x = start; x < numSeats; x++) {
           if (seats[x] == false) {
               return true;
           }
       }
       
       return false;
   } 
   
   /* Determine if Plane is full, return true if full, and false if seats are available */
   public boolean isPlaneFull() {
       for (int x = 0; x < numSeats; x++) {
           if (seats[x] == false) {
               return false;
           }
       }
       
       return true;
   }
   
   /* Output airline summary to the console */
   
   public void printSummary() {
       System.out.printf("Flight number: %1$s \n", flightNum);
       System.out.printf("Route: %1$s \n", route);
       System.out.printf("Total seats: %1$d \n", numSeats);
       System.out.printf("Available seats: %1$d \n", numAvailable);
   } 
   
   /* Output airline number and route to the console */
   
   public void printFlightNumberAndRoute() {
       System.out.println("#" + flightNum + ": " + route);
   }    
   
   /* Check availability of a single seat
        @param seatNum: seat number to verify
        @return:        true of seat is available */
   
   public boolean checkReservation(int seatNum) {
        return seats[seatNum - 1];
   }
   
   
   /* Check if all seats are full 
        @return: true if full */
   
   public boolean isFull() {
       if(numAvailable == 0)
            return true;
       else
           return false;
   }
   
   
   public int getFirstClass() {
       return firstClass;
   }
   

   public void setFirstClass(int newValue) {
       firstClass = newValue;
   }
   

   public int getEconomy() {
       return economy;
   }

   
   public void setEconomy(int newValue) {
       economy = newValue;
   }
   

   public int getNumSeats() {
       return numSeats;
   }
   

   public void setNumSeats(int newValue) {
       numSeats = newValue;
   }
   
   
   public int getFlightNum() {
       return flightNum;
   }
   
   
   public void setFlightNum(int newValue) {
       flightNum = newValue;
   }
   
   
   public String getRoute() {
       return route;
   }
   
   
   public void setRoute(String newRoute) {
       route = newRoute;
   }
   
   public int getLastSeatReserved() {
       return lastSeatReserved + 1;
   }
   
   
   private int flightNum;
   private boolean seats[];
   private int firstClass;
   private int economy;
   private int numSeats;
   private int numAvailable;
   private String route;
   private int lastSeatReserved;

} 

